package com.young.commom.httpclient.components;

import com.young.commom.httpclient.RequestHeaderInterceptor;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.protocol.HttpContext;

import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 1:11 PM
 */
public class HttpComponentsHttpRequestInterceptor extends RequestHeaderInterceptor implements HttpRequestInterceptor {
    public HttpComponentsHttpRequestInterceptor() {
    }

    public void process(final HttpRequest request, final HttpContext context) {
        String method = request.getRequestLine().getMethod();
        if (!method.equalsIgnoreCase("CONNECT")) {
            Map<String, String> headers = this.requestHeaders();
            if (headers != null && !headers.isEmpty()) {
                headers.forEach(request::setHeader);
            }
        }
    }
}
